<template>
    <div class="menuList">
        <!--背景卡片-->
        <el-card class="box-card">
            <el-form size="mini" ref="souSuoForm" :inline="true" @submit.native.prevent :model="souSuoFormInline"
                     class="demo-form-inline" style="margin-bottom: 0px">
                <el-form-item label="菜单名称：" prop="name">
                    <el-input v-model="souSuoFormInline.name" @keyup.enter.native="initTable"
                              placeholder="请输入菜单名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="initTable2()" icon="el-icon-search">查询</el-button>
                    <el-button @click="resetForm('souSuoForm')" icon="el-icon-refresh">重置</el-button>
                    <el-button type="primary" @click="displayInsertForm=true" size="mini" icon="el-icon-plus">新增
                    </el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <el-table
                    :data="tableData"
                    border
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    :default-expand-all="false"
                    :tree-props="{children: 'children'}">
                <el-table-column
                        align="center"
                        type="index"
                        width="50px"
                        label="序号">
                </el-table-column>

                <el-table-column
                        prop="name"
                        label="菜单名称"
                >
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="path"
                        label="路由地址">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="permissionValue"
                        label="接口地址">
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="type"
                        label="类型">
                    <template slot-scope="scope">
                        <span v-if="scope.row.type === 1">菜单</span>
                        <span v-else-if="scope.row.type === 2">按钮</span>
                    </template>
                </el-table-column>
                <el-table-column
                        align="center"
                        prop="icon"
                        label="图标">
                    <template slot-scope="scope">
                        <i :class="scope.row.icon"></i>
                    </template>
                </el-table-column>

                <el-table-column
                        align="center"
                        fixed="right"
                        label="操作"
                        width="250px">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.type==1" type="primary" size="mini" @click="insertClick(scope.row)">
                            添加
                        </el-button>
                        <el-button type="success" v-if="scope.row.type===1" size="mini" @click="update(scope.row)">修改
                        </el-button>
                        <el-button type="success" v-if="scope.row.type!==1" style="width: 123px" size="mini"
                                   @click="update(scope.row)">修改
                        </el-button>
                        <el-button type="danger" size="mini" @click="del(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-divider><i class="el-icon-moon-night"></i></el-divider>
            <!--分页栏-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    style="float: right;margin-top: 20px; margin-bottom: 22px"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 15]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>

            <!-- 新增主目录的弹出层（新增按钮）-->
            <el-dialog
                    title="新增主目录"
                    :visible.sync="displayInsertForm"
                    width="28%"
                    :before-close="handleInsertClose">
                <el-form ref="insertForm" :model="insertFormDate" :rules="formRules" label-width="80px">
                    <el-form-item label="菜单名称" prop="name">
                        <el-input v-model="insertFormDate.name" style="width: 300px"></el-input>
                    </el-form-item>
                    <el-form-item label="路由地址" prop="path">
                        <el-input v-model="insertFormDate.path" style="width: 300px"></el-input>
                    </el-form-item>
                    <el-form-item label="图标" prop="icon">
                        <el-radio-group v-model="insertFormDate.icon">
                            <el-radio label="el-icon-s-unfold"><i class="el-icon-s-unfold"></i></el-radio>
                            <el-radio label="el-icon-s-operation"><i class="el-icon-s-operation"></i></el-radio>
                            <el-radio label="el-icon-s-fold"><i class="el-icon-s-fold"></i></el-radio>
                            <el-radio label="el-icon-s-home"><i class="el-icon-s-home"></i></el-radio>
                            <el-radio label="el-icon-menu"><i class="el-icon-menu"></i></el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
                <el-row style="margin-top:17px;">
                    <el-button style="float:right;margin-left:6px;" size="mini" type="danger" plain
                               @click="handleInsertClose()">取 消
                    </el-button>
                    <el-button style="float:right" size="mini" type="primary" @click="insertFormSubmit()">确 定
                    </el-button>
                </el-row>
            </el-dialog>


            <!--主目录或菜单修改的弹出层-->
            <el-dialog
                    title="修改"
                    :visible.sync="displayUpdateForm"
                    width="28%"
                    :closed="handleUpdateClose">
                <el-form ref="updateForm" :model="updateFormDate" :rules="formRules" label-width="80px"
                         @submit.native.prevent>
                    <el-form-item label="菜单名称" prop="name">
                        <el-input v-model="updateFormDate.name" style="width: 300px"></el-input>
                    </el-form-item>
                    <el-form-item label="路由地址" prop="path">
                        <el-input v-model="updateFormDate.path" @keyup.enter.native="updateFormSubmit"
                                  style="width: 300px"></el-input>
                    </el-form-item>
                </el-form>
                <el-row style="margin-top:17px;">
                    <el-button style="float:right;margin-left:6px;" size="mini" type="danger" plain
                               @click="handleUpdateClose()">取 消
                    </el-button>
                    <el-button style="float:right" size="mini" type="primary" @click="updateFormSubmit()">确 定
                    </el-button>
                </el-row>
            </el-dialog>

            <!--按钮修改的弹出层-->
            <el-dialog
                    title="修改"
                    :visible.sync="anNiuUpdateForm"
                    width="30%"
                    :closed="anNiuUpdateClose">
                <el-form ref="updateForm" :model="updateFormDate" :rules="formRules" label-width="80px">
                    <el-form-item label="菜单名称" prop="name">
                        <el-input v-model="updateFormDate.name" style="width: 300px"></el-input>
                    </el-form-item>
                    <el-form-item label="接口地址" prop="permissionValue">
                        <el-input v-model="updateFormDate.permissionValue" style="width: 300px"></el-input>
                    </el-form-item>
                </el-form>
                <el-row style="margin-top:17px;">
                    <el-button style="float:right;margin-left:6px;" size="mini" type="danger" plain
                               @click="anNiuUpdateClose()">取 消
                    </el-button>
                    <el-button style="float:right" size="mini" type="primary" @click="updateFormSubmit()">确 定
                    </el-button>
                </el-row>
            </el-dialog>


            <!--按钮==>  添加菜单或按钮-->
            <el-dialog
                    title="添加"
                    :visible.sync="DisplayInsertPermission"
                    width="30%"
                    :before-close="handleInsertPermissionClose">
                <el-form ref="insertPermissionForm" :model="insertPermissionData" style="margin: auto;width: 430px"
                         :rules="formRules" label-width="80px">
                    <el-form-item label-width="100px" label="菜单名称" prop="type">
                        <el-radio-group v-model="insertPermissionData.type">
                            <el-radio :label="1">菜单</el-radio>
                            <el-radio :label="2">按钮</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label-width="100px" label="菜单名称" prop="name" v-if="insertPermissionData.type==1">
                        <el-input v-model="insertPermissionData.name" style="width: 300px"></el-input>
                    </el-form-item>
                    <el-form-item label-width="100px" label="按钮名称" prop="name" v-if="insertPermissionData.type==2">
                        <el-input v-model="insertPermissionData.name" style="width: 300px"></el-input>
                    </el-form-item>
                    <el-form-item label-width="100px" label="路由地址" prop="path" v-if="insertPermissionData.type==1">
                        <el-input v-model="insertPermissionData.path" style="width: 300px"></el-input>
                    </el-form-item>
                    <el-form-item label-width="100px" label="接口地址" prop="permissionValue"
                                  v-if="insertPermissionData.type==2">
                        <el-input v-model="insertPermissionData.permissionValue" style="width: 300px"></el-input>
                    </el-form-item>
                    <el-form-item label="图标" prop="icon" v-if="insertPermissionData.type==1">
                        <el-radio-group v-model="insertPermissionData.icon">
                            <el-radio label="el-icon-s-unfold"><i class="el-icon-s-unfold"></i></el-radio>
                            <el-radio label="el-icon-s-operation"><i class="el-icon-s-operation"></i></el-radio>
                            <el-radio label="el-icon-s-fold"><i class="el-icon-s-fold"></i></el-radio>
                            <el-radio label="el-icon-s-home"><i class="el-icon-s-home"></i></el-radio>
                            <el-radio label="el-icon-menu"><i class="el-icon-menu"></i></el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
                <el-row style="margin-top:17px;">
                    <el-button style="float:right;margin-left:6px;" size="mini" type="danger" plain
                               @click="handleInsertPermissionClose()">取 消
                    </el-button>
                    <el-button style="float:right" size="mini" type="primary" @click="insertPermission()">确 定
                    </el-button>
                </el-row>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "MenuList",
    data() {
        return {
            //搜索表单
            souSuoFormInline: {
                name: ''
            },
            //表格数据
            tableData: [],
            //分页信息
            currentPage: 1,
            //当前页码数
            pageSize: 5,
            //总条数
            total: 0,

            //新增表单数据
            displayInsertForm: false,
            insertFormDate: {
                name: '',
                path: '',
                remark: '',
                pid: 1,
                type: 1,
                icon: '',
            },

            //主目录或菜单修改表单的层显示
            displayUpdateForm: false,
            //按钮修改表单的层显示
            anNiuUpdateForm: false,
            updateFormDate: {
                name: '',
                path: '',
                remark: '',
                permissionValue: '',
            },

            //修改权限
            displayUpdatePermission: false,
            ids: [],

            //按钮新增
            DisplayInsertPermission: false,
            insertPermissionData: {
                type: 1,
                name: '',
                path: '',
                remark: '',
                pid: 1,
                permissionValue: '',
                icon: '',
            },
            ffType: '',

            //表单的规则
            formRules: {
                name: [
                    {required: true, message: '请输入菜单名称', trigger: 'blur'},
                ],
                path: [
                    {required: true, message: '请输入路由地址', trigger: 'blur'},
                ],
                permissionValue: [
                    {required: true, message: '请输入接口地址', trigger: 'blur'},
                ],
                type: [
                    {required: true, message: '请选择类型', trigger: 'blur'},
                ],
            }
        }
    },
    //页面加载时初始化表格数据
    created() {
        this.initTable();
    },
    methods: {
        //页头的点击条状
        goBack() {
            this.$router.push("/houHome")
        },
        initTable2() {
            this.currentPage=1;
            this.initTable();
        },
        //获取表格数据
        initTable() {
            const that = this;
            that.$http.post(`system/permission/findAllByPage/${that.currentPage}/${that.pageSize}`, this.souSuoFormInline).then(function (response) {
                if (response.data.code === 200) {
                    that.tableData = response.data.data.records;
                    that.total = response.data.data.total;
                    that.$refs.souSuoForm.resetFields();//将搜索框清空
                }
            })
        },
        //页数改变时
        handleCurrentChange(val) {
            this.currentPage = val;
            this.initTable();
        },
        //每页数量发生改变时
        handleSizeChange(val) {
            this.pageSize = val;
            this.initTable();
        },
        //重置搜索表单
        resetForm(ref) {
            this.currentPage = 1
            this.initTable();
        },

        //删除按钮事件--循环遍历删除的行，判断是否有子，如果有，将子id存入ids内一起删除
        getIds(row) {
            this.ids.push(row.id);
            if (row.children.length > 0) {
                row.children.forEach(chi => {
                    this.getIds(chi);
                })
            }
        },
        //删除按钮事件，执行删除
        del(row) {
            this.$confirm(`确定删除该${row.type === 1 ? "菜单" : "按钮"}吗？, 是否继续?`, '提示', {
                cancelButtonText: '取消',
                confirmButtonText: '确定',
                type: 'warning'
            }).then(() => {
                this.ids = [];
                this.getIds(row);
                this.$http.post(`system/permission/deleteById`, {"permissionIds": this.ids}).then((resp) => {
                    if (resp.data.code === 200) {
                        this.$message.success(resp.data.msg);
                        this.initTable();
                    } else
                        this.$message.error(resp.data.msg);
                    this.initTable();
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '删除已取消'
                });
            });
        },
        //新增表单关闭前回调
        handleInsertClose() {
            this.displayInsertForm = false;
            this.$refs.insertForm.resetFields();
        },
        //新增表单提交
        insertFormSubmit() {
            const that = this;
            this.$refs.insertForm.validate(valid => {
                if (valid) {
                    that.$http.put(`system/permission/insert`, this.insertFormDate).then(function (resp) {
                        if (resp.data.code === 200) {
                            that.initTable();
                            that.$message.success(resp.data.msg);
                            that.handleInsertClose();
                        } else {
                            that.$message.error(resp.data.msg);
                        }
                    })
                }
            })
        },
        //修改按钮
        update(row) {
            this.updateFormDate = row;
            if (row.type === 1) {//判断是主目录还是按钮
                this.displayUpdateForm = true;
            } else if (row.type === 2) {
                this.anNiuUpdateForm = true;
            }
        },
        //主目录或菜单修改表单关闭回调
        handleUpdateClose() {
            this.displayUpdateForm = false;
            this.$refs.updateForm.resetFields();
        },
        //按钮修改表单关闭回调
        anNiuUpdateClose() {
            this.anNiuUpdateForm = false;
            this.$refs.updateForm.resetFields();
        },
        //修改表单提交事件
        updateFormSubmit() {
            this.$refs.updateForm.validate(valid => {
                if (valid) {
                    this.$http.post(`system/permission/update/`, this.updateFormDate).then((resp) => {
                        if (resp.data.code === 200) {
                            this.$message.success(resp.data.msg);
                            this.initTable();
                            this.handleUpdateClose();
                            this.anNiuUpdateClose();
                        } else {
                            this.$message.error(resp.data.msg);
                        }
                    })
                }
            })
        },

        //行内添加按钮点击
        insertClick(row) {
            this.DisplayInsertPermission = true;
            this.insertPermissionData.pid = row.id;
            if (row.typeName === "主目录") {
                this.ffType = 1;
            } else if (row.typeName === "菜单") {
                this.ffType = 2;
            }


        },
        //按钮添加方法
        insertPermission() {
            const that = this;
            this.$refs.insertPermissionForm.validate(valid => {
                if (valid) {
                    that.$http.put(`system/permission/insert`, this.insertPermissionData).then(function (resp) {
                        if (resp.data.code === 200) {
                            that.initTable();
                            that.$message.success(resp.data.msg);
                            that.handleInsertPermissionClose();
                        } else {
                            that.$message.error(resp.data.msg);
                        }
                    })
                }
            })
        },
        //添加关闭前回调
        handleInsertPermissionClose() {
            this.DisplayInsertPermission = false;
            this.insertPermissionData.path = '';
            this.insertPermissionData.permissionValue = '';
            this.$refs.insertPermissionForm.resetFields();

        }
    }
}
</script>
